package cn.net.isir.service2;

import com.github.pagehelper.PageInfo;
import cn.net.isir.core.BasicService2;
import cn.net.isir.core.SetBasicDao;
import cn.net.isir.dao.NotifyTaskDao;
import cn.net.isir.dao.OrderDao;
import cn.net.isir.entity.NotifyTask;
import cn.net.isir.entity.Order;
import cn.net.isir.entity.Shop;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author liuxubo
 * @filename ServingManageService.java
 * @package cn.net.isir.service2
 * @email lxb@isir.net.cn
 * @date 2019/01/02
 * @comment 查询通知service
 * @since 1.8
 */
@Service
public class ServingManageService extends BasicService2<NotifyTask> {
    @SetBasicDao
    @Resource
    private NotifyTaskDao notifyTaskDao;
    @Resource
    private OrderDao orderDao;

    /**
     * 查询列表
     * @param shopList 店铺列表
     * @param notifyTask 通知任务
     * @return 通知列表
     */
    public PageInfo getNotifyTask(List<Shop> shopList, NotifyTask notifyTask) {
        List<NotifyTask> notifyTaskList = new ArrayList<>();
        for (Shop shop : shopList) {
//            NotifyTask notifyTask = new NotifyTask();
            notifyTask.setShopId(shop.getId());
            notifyTask.setPage_sql(null);
            List<NotifyTask> subNotifyTaskList =notifyTaskDao.selectList(notifyTask);
            for (NotifyTask nt : subNotifyTaskList) {
                Order order = orderDao.selectByOrderNo(nt.getOrderNo());
                nt.setShopName(shop.getName());
                nt.setSerialNo(order.getSerialNumber());
                nt.setPayTime(order.getPayTime());
            }
            notifyTaskList.addAll(subNotifyTaskList);
        }
        return new PageInfo(notifyTaskList);
    }

    /**
     * 发通知
     * @param notifyId 通知id
     * @return 修改结果
     */
    public Object notify(String notifyId,Date servingTime) {
        NotifyTask notifyTask = notifyTaskDao.selectByPrimaryKey(notifyId);
        if (notifyTask == null) {
            return 0;
        }
        notifyTask.setPlatformStatus("notified");
        notifyTask.setPlatformTime(servingTime);
        return notifyTaskDao.update(notifyTask);
    }
}
